﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBControl;
using System.Data;
using System.Threading;

namespace ProjectX
{
    public partial class TBJump : Errorbase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userNick"] == null)
            {
                Response.Redirect("/login.html");
            }
        }

        private void AddUsertoTag()
        {
            SqlDbHelper sdh = new SqlDbHelper();
            string getTbname = "SELECT Tbname FROM UserRegister WHERE userName ='" + Session["userNick"] + "'";
            string getSellername = "SELECT Releasename,Tradelink FROM SellerinfoR WHERE Id='" + Request.QueryString["sellerid"] + "'";
            DataTable sellertable = sdh.ExecuteDataTable(getSellername);
            string sellerName = sellertable.Rows[0]["Releasename"].ToString();
            string tradeLink = sellertable.Rows[0]["Tradelink"].ToString();
            string userTbname = sdh.ExecuteScalar(getTbname).ToString();
            ipclick();

            if (!userTbname.Equals(""))
            {
                if (!sellerName.Equals(""))
                {
                    string getTag = "SELECT Tagid FROM GetTagFromSeller WHERE SellerName = '" + sellerName + "'";
                    string getSellSessionkey = "SELECT SessionKey FROM UserSession WHERE UsNick = '" + sellerName + "'";
                    string SellSessionkey = sdh.ExecuteScalar(getSellSessionkey).ToString();
                    int Tagid = int.Parse(sdh.ExecuteScalar(getTag).ToString());
                    Marketing market = new Marketing(SellSessionkey);
                    if (market.addTargetuser(Tagid, userTbname))
                    {
                        Response.Redirect(tradeLink);
                    }
                    else
                    {
                        Response.Redirect("/404.htm");
                    }
                }
            }
            else
            {
                Response.Redirect(tradeLink);
            }
        }

        protected void jumpBtn_Click(object sender, EventArgs e)
        {
            AddUsertoTag();
        }

        private string ipclick()
        {
            string sellid = Request.QueryString["sellerid"];
            SqlDbHelper sdh = new SqlDbHelper();
            string user = Session["userNick"].ToString();
            string ip = GetUserIp();
            string checkip = "SELECT * FROM Userip WHERE username = '" + user + "' AND sellid = '" + sellid + "'";
            try
            {
                DataTable dt = sdh.ExecuteDataTable(checkip);
                if (dt.Rows.Count == 0)
                {
                    string insertip = "INSERT INTO Userip(username,ip,sellid,count) VALUES('" + user + "','" + ip + "','" + sellid + "',1)";
                    return sdh.ExecuteNonQuery(insertip).ToString();
                }
                else
                {
                    if (!dt.Rows[0]["ip"].ToString().Equals(ip))
                    {
                        string updateip = "UPDATE Userip SET ip='" + ip + "',count=count + 1 WHERE username = '" + user + "' AND sellid = '" + sellid + "'";
                        return sdh.ExecuteNonQuery(updateip).ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        private string GetUserIp()
        {
            // 穿过代理服务器取远程用户真实IP地址
            string Ip = string.Empty;
            if (Request.ServerVariables["HTTP_VIA"] != null)
            {
                if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
                {
                    if (Request.ServerVariables["HTTP_CLIENT_IP"] != null)
                    {
                        Ip = Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
                    }
                    else
                    {
                        if (Request.ServerVariables["REMOTE_ADDR"] != null)
                        {
                            Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
                        }
                        else
                        {
                            Ip = "";
                        }
                    }
                }
                else
                {
                    Ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
            }
            else if (Request.ServerVariables["REMOTE_ADDR"] != null)
            {
                Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            else
            {
                Ip = "";
            }
            return Ip;
        }
    }
}